package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.controller.es.EsUnService;
import com.ruoyi.system.controller.es.dto.EsSearch;
import com.ruoyi.system.domain.vo.DeleteIdVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FyPolicy;
import com.ruoyi.system.service.IFyPolicyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 政策Controller
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/system/policy")
public class FyPolicyController extends BaseController
{
    @Autowired
    private IFyPolicyService fyPolicyService;
    @Autowired
    private EsUnService esUnService;
    /**
     * 查询政策列表
     */
//    @PreAuthorize("@ss.hasPermi('system:policy:list')")
    @GetMapping("/list")
    public TableDataInfo list(FyPolicy fyPolicy)
    {
        startPage();
        List<FyPolicy> list = fyPolicyService.selectFyPolicyList(fyPolicy);
        return getDataTable(list);
    }

    /**
     * 导出政策列表
     */
//    @PreAuthorize("@ss.hasPermi('system:policy:export')")
    @Log(title = "政策", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FyPolicy fyPolicy)
    {
        List<FyPolicy> list = fyPolicyService.selectFyPolicyList(fyPolicy);
        ExcelUtil<FyPolicy> util = new ExcelUtil<FyPolicy>(FyPolicy.class);
        util.exportExcel(response, list, "政策数据");
    }

    /**
     * 获取政策详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:policy:query')")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long policyId)
    {
        return success(fyPolicyService.selectFyPolicyByPolicyId(policyId));
    }

    /**
     * 新增政策
     */
//    @PreAuthorize("@ss.hasPermi('system:policy:add')")
    @Log(title = "政策", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FyPolicy fyPolicy)
    {
        fyPolicy.setPolicyId(Long.valueOf(DateUtils.dateTimeNow()));
        EsSearch esSearch=new EsSearch();
        esSearch.setId(fyPolicy.getPolicyId());
        esSearch.setLanmu("政策");
        esSearch.setLanmuId(4);
        esSearch.setTitle(fyPolicy.getPolicyTitle());
        esSearch.setContent(fyPolicy.getContent());
        esUnService.save(esSearch);
        return toAjax(fyPolicyService.insertFyPolicy(fyPolicy));
    }

    /**
     * 修改政策
     */
//    @PreAuthorize("@ss.hasPermi('system:policy:edit')")
    @Log(title = "政策", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody FyPolicy fyPolicy)
    {
        EsSearch esSearch=new EsSearch();
        esSearch.setId(fyPolicy.getPolicyId());
        esSearch.setLanmu("政策");
        esSearch.setLanmuId(4);
        esUnService.delete(esSearch);
        esSearch.setTitle(fyPolicy.getPolicyTitle());
        esSearch.setContent(fyPolicy.getContent());
        esUnService.save(esSearch);
        return toAjax(fyPolicyService.updateFyPolicy(fyPolicy));
    }

    /**
     * 删除政策
     */
//    @PreAuthorize("@ss.hasPermi('system:policy:remove')")
    @Log(title = "政策", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DeleteIdVo deleteIdVo)
    {
        return toAjax(fyPolicyService.deleteFyPolicyByPolicyIds(deleteIdVo.getPolicyIds()));
    }
}
